class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return head
        last = head
        left = head
        right = head.next
        flag = False
        while right:
            if right.val == left.val:
                flag = True
                left.next = right.next
                right = right.next
            else:
                if flag:
                    if last == head:
                        head = right
                        last = right
                        left = right
                        right = right.next
                    else:
                        last.next = right
                        left = right
                        right = right.next
                    flag = False
                else:
                    last = left
                    left = right
                    right = right.next
        if flag:
            last.next = None
        return head
